package com.example.optimizaapp;


import com.example.optimizaapp.domain.Incidente;
import com.example.optimizaapp.domain.ManejadorIncidente;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class BuscarIncidenteActivity extends Activity {
	ManejadorIncidente mi= ManejadorIncidente.getManejadorIncidente();
			
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_buscar_incidente);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	    // TODO Auto-generated method stub
	}

	/** Called when the user clicks the button */
	public void pressedBuscarIncidente(View view) {
		EditText nroIncidente = (EditText) findViewById(R.id.txtNroIncidente);
		if (!nroIncidente.getText().toString().equals("")){
			int numIncidente = Integer.parseInt(nroIncidente.getText().toString());
			if (buscarIncidente(numIncidente) != -1){
				Intent intent = new Intent(getBaseContext(), DetalleIncidente.class);
	        	intent.putExtra("INDEX", buscarIncidente(numIncidente));
	        	startActivity(intent);
			}else{
				new AlertDialog.Builder(this).setTitle("Error").setMessage("No se encontro incidente").setNeutralButton("OK", null).show();
			}
		}else{
			new AlertDialog.Builder(this).setTitle("Error").setMessage("Debe ingresar el numero de incidente").setNeutralButton("OK", null).show();
		}
		
	}
	
	public int buscarIncidente(int num){
		for (int i = 0 ; i<mi.getIncidentes().size(); i++){
			Incidente in = (Incidente)mi.getIncidentes().get(i);
			if (in.getIdIncidente() == num){
				return i;
			}
		}
		
		return -1;
	}
}
